import api from "../../utils/api";

// pages/userCenter/userCenter.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // name: wx.getStorageSync("userName"),
    // workNo: wx.getStorageSync("workNo"),
    // department: "",
    // phoneNo: "",
    userInfo: {},
    departmentsList: [],
    departmentSelect: '',
    departmentHide: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getUserInfo()
    this.getDepartment()
  },

  getUserInfo: function () {
    let _this = this
    wx.request({
      url: api.getUserInfo + '?workno=' + wx.getStorageSync("workNo"),
      method: "GET",
      success(res) {
        if (res.data.state == 1) {
          // _this.setData({
          //   department: res.data.data[0].depName,
          //   phoneNo: res.data.data[0].mobile,
          // })
          _this.setData({
            userInfo: res.data.data[0]
          })
        }
      }
    })
  },
  getDepartment: function () {
    let _this = this
    wx.request({
      url: api.getDepList,
      method: "GET",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.state == 1) {
          _this.setData({
            departmentsList: res.data.data
          })
        }
      }
    })
  },
  radioChange: function (e) {
    this.setData({
      departmentSelect: e.detail.value
    })
  },
  confirm: function () {
    let _this = this
    wx.request({
      url: api.updateDep,
      data: {
        workno: wx.getStorageSync("workNo"),
        department: this.data.departmentSelect
      },
      header: {
        'content-type': 'application/json', // 默认值
      },
      method: "POST",
      success(res) {
        if (res.data.state == 1) {
          _this.data.departmentsList.forEach(item => {
            if (item.department == _this.data.departmentSelect) {
              let userInfo = _this.data.userInfo
              userInfo.depName = item.name
              _this.setData({
                userInfo: userInfo
              })
            }
          })
          _this.hideModal ()
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 显示遮罩层
  showModal: function (e) {
    this.data.departmentsList.forEach(item => {
      if (item.name == this.data.userInfo.depName) {
        this.setData({
          departmentSelect: item.department
        })
      }
    })
    var that = this;

    that.setData({
      departmentHide: false
    })
    var animation = wx.createAnimation({
      duration: 600, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
      timingFunction: 'ease', //动画的效果 默认值是linear
    })
    this.animation = animation
    setTimeout(function () {
      that.fadeIn(); //调用显示动画
    }, 200)
  },

  // 隐藏遮罩层
  hideModal: function () {
    var that = this;
    var animation = wx.createAnimation({
      duration: 800, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
      timingFunction: 'ease', //动画的效果 默认值是linear
    })
    this.animation = animation
    that.fadeDown(); //调用隐藏动画   
    setTimeout(function () {
      that.setData({
        departmentHide: true
      })
    }, 750) //先执行下滑动画，再隐藏模块
  },

  //动画集
  fadeIn: function () {
    this.animation.translateY(0).step()
    this.setData({
      animationData: this.animation.export() //动画实例的export方法导出动画数据传递给组件的animation属性
    })
  },
  fadeDown: function () {
    this.animation.translateY(700).step()
    this.setData({
      animationData: this.animation.export(),
    })
  },

  

  inputChange: function (e) {
    this.setData({
      phoneNo: e.detail.value
    })
  },

})
